package testLayer;

import static org.junit.Assert.*;
import modelLayer.Block;
import modelLayer.Schedule;
import modelLayer.TeachingBlock;
import modelLayer.Class;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import controlLayer.ClassCtr;
import controlLayer.ScheduleCtr;
import controlLayer.TeachingBlockCtr;
import dbLayer.DBBlock;
import dbLayer.DBTeachingBlock;

public class ScheduleControllerTest {
	
	private ScheduleCtr schCtr;
	private boolean retrieveAssociation = false;
	
	
	@Before
	public void setUp() throws Exception {
		schCtr = new ScheduleCtr();
		testCreateSchedule();
		
	}

	@After
	public void tearDown() throws Exception {
		
		testRemoveSchedule();
		
	}

	@Test
	public void testScheduleCtr() {
		
		assertNotNull(schCtr);
		
	}
	
	@Test
	public void testCreateSchedule() throws Exception {
		
		System.out.println("[SCHEDULECTR] CREATE TEST START.");
		String className = "men0811";
		int numberOfBlocks = 36;
		assertTrue(schCtr.createSchedule(className, numberOfBlocks) > 0);
		new DBBlock().update(schCtr.findScheduleByName("men0811", false).getId(), 36, new DBTeachingBlock().find("3GeoBio-2", false));
		System.out.println("[SCHEDULECTR] CREATE TEST COMPLETE.");
		
	}

	@Test
	public void testFindScheduleByName() {
		
		System.out.println("[SCHEDULECTR] FINDBYNAME TEST START.");
		assertTrue(schCtr.findScheduleByName("men0811", retrieveAssociation) != null);
		System.out.println("[SCHEDULECTR] FINDBYNAME TEST COMPLETE.");
		
	}

	@Test
	public void testFindScheduleById() {
		
		System.out.println("[SCHEDULECTR] FINDBYID TEST START.");
		Schedule sch = schCtr.findScheduleByName("men0811", retrieveAssociation);
		int id = sch.getId();
		assertTrue(schCtr.findScheduleById(id, retrieveAssociation) != null);
		System.out.println("[SCHEDULECTR] FINDBYNAME TEST COMPLETE.");
		
	}

	@Test
	public void testGetAll() {
		
		System.out.println("[SCHEDULECTR] GETALL TEST START.");
		assertNotNull(schCtr.getAll(retrieveAssociation));
		System.out.println("[SCHEDULECTR] GETALL TEST COMPLETE.");
		
	}

	@Test
	public void testRemoveSchedule() throws Exception {
		
		System.out.println("[SCHEDULECTR] REMOVE TEST START.");
		Schedule sch = schCtr.findScheduleByName("men0811", retrieveAssociation);
		assertTrue(schCtr.removeSchedule(sch) > 0);
		System.out.println("[SCHEDULECTR] REMOVE TEST COMPLETE.");
		
	}

//	@Test
//	public void testMoveTeachingBlockBestCase() throws Exception {
//		
//		System.out.println("[SCHEDULECTR] moveTeachingBlockBestCase TEST START.");
//		Class c = new ClassCtr().findClass("men0811", false);
//		Schedule sch = this.schCtr.findScheduleByName(c.getClassName(), false);
//		TeachingBlock tb = new TeachingBlockCtr().findTeachingBlockByName("3HisL01-1", true);
//		Block b = new DBBlock().find(sch.getId(), 5, false);
//		assertTrue(schCtr.moveTeachingBlock(c.getClassName(), tb.getId(), b.getBlockNumber()));
//		System.out.println("[SCHEDULECTR] moveTeachingBlockBestCase TEST COMPLETE.");
//		
//	}
	
//	@Test
//	public void testMoveTeachingBlockConflict() throws Exception {
//		
//		System.out.println("[SCHEDULECTR] moveTeachingBlockConflict TEST START.");
//		Class c = new ClassCtr().findClass("men0811", false);
//		Schedule sch = this.schCtr.findScheduleByName(c.getClassName(), false);
//		TeachingBlock tb = new TeachingBlockCtr().findTeachingBlockByName("3HisL01-2", true);
//		Block b = new DBBlock().find(sch.getId(), 2, false);
//		assertFalse(schCtr.moveTeachingBlock(c.getClassName(), tb.getId(), b.getBlockNumber()));
//		System.out.println("[SCHEDULECTR] moveTeachingBlockConflict TEST COMPLETE.");
//		
//	}

//	@Test
//	public void testUpdateSchedule() throws Exception {
//	
//		testMoveTeachingBlockBestCase();
//		System.out.println("[SCHEDULECTR] UPDATESCHEDULE TEST START.");
//		//this.sch = this.schCtr.findScheduleByName("men0811", false);
//		this.sch = schCtr.getSchedule();
//		System.out.println(sch);
//		//System.out.println("ScheduleID: " + sch.getId());
//		assertTrue(schCtr.updateSchedule(this.sch) > 0);
//		System.out.println("[SCHEDULECTR] UPDATESCHEDULE TEST COMPLETE.");
//	
//	}
	
//	@Test
//	public void testUpdateBlock() throws Exception {
//		
//		System.out.println("[SCHEDULECTR] UPDATEBLOCK TEST START.");
//		Schedule sch = schCtr.findScheduleByName("men0811", false);
//		System.out.println("Schedule id: " + sch.getId());
//		TeachingBlock tb = new TeachingBlockCtr().findTeachingBlockByName("3HisL01-1", true);
//		Block b = new DBBlock().find(sch.getId(), 2, retrieveAssociation);
//		assertTrue(schCtr.updateBlock(sch, tb, b) > 0);
//		System.out.println("[SCHEDULECTR] UPDATEBLOCK TEST COMPLETE.");
//		
//	}
	
//	@Test
//	public void testUpdateBlockWithNull() throws Exception {
//		
//		System.out.println("[SCHEDULECTR] updateBlockWithNull TEST START.");
//		Schedule sch = schCtr.findScheduleByName("men0811", false);
//		System.out.println("Schedule id: " + sch.getId());
//		TeachingBlock tb = null;
//		Block b = new DBBlock().find(sch.getId(), 36, retrieveAssociation);
//		assertTrue(schCtr.updateBlock(sch, tb, b) > 0);
//		System.out.println("[SCHEDULECTR] updateBlockWithNull TEST COMPLETE.");
//		
//	}
	
//	@Test
//	public void testGetAllNonConflictingBySchedule() {
//	
//		System.out.println("[SCHEDULECTR/DBBLOCK] testGetAllNonConflictingBySchedule TEST START.");
//		Schedule sch = this.schCtr.findScheduleByName("men0811",false);
//		assertNotNull(new DBBlock().getAllNonConflictingBySchedule(sch.getId(), retrieveAssociation, 2));
//		System.out.println(new DBBlock().getAllNonConflictingBySchedule(sch.getId(), retrieveAssociation, 2));
//		System.out.println("[SCHEDULECTR/DBBLOCK] testGetAllNonConflictingBySchedule TEST COMPLETE.");
//	
//	}
}
